<template>
  <div v-if="isInit">
    <Checkbox size="small" v-model="isExportAllQuestions">
      {{ isExportAllQuestions ? "导出所有题目" : "点击导出题目" }}
    </Checkbox>
    ---
    <Checkbox size="small" v-model="isAnonymousQuestions">匿名筛选</Checkbox>
    <Checkbox size="small" v-model="isHaveNO" v-if="isAnonymousQuestions"
      >包含编号</Checkbox
    >
    共<InputNumber size="small" class="w60" v-model="selectNums"></InputNumber
    >人
    <template v-if="isExportAllQuestions">
      <p v-for="(num, index2) in selectPeopleNums" :key="'p_' + index2">
        评选人{{ index2 + 1 }}：
        <Select
          transfer
          filterable
          filter-by-label
          class="w200"
          v-model="selectAllPeople[index2]"
          @on-change="handleChangeSelectPeople()"
        >
          <Option
            v-for="(people, pIndex) in checkOptionPeoples('', index2)"
            :key="'num_' + pIndex"
            :value="people.qq"
            >{{ people.nickname }}</Option
          >
        </Select>
        - [<a class="red" @click="doExport(null, index2)">下载</a>]
        {{ (answerObj = answerSelectedObj[index2 + ""]) && "" }}
        <template v-if="answerObj">
          - 数量：{{ answerObj.qqs.length }}
          <template v-if="answerObj.moveNum !== -1">
            - 作业挪到第 {{ answerObj.moveNum + 1 }} 位小伙伴
          </template>
        </template>
      </p>
    </template>
    <template v-if="!isExportAllQuestions">
      <div
        class="questions mar10t"
        v-for="(item, index) in questions"
        :key="'q_' + index"
      >
        <p>
          [<a class="red" @click="doExport(index)">下载</a>] - [<a
            class="red"
            @click="showQuestion(index)"
            >查看</a
          >] -
          <a @click="showQuestion(index)">{{ item.question }}</a>
        </p>
        <p
          v-for="(num, index2) in selectPeopleNums"
          :key="'p_' + index + '_' + index2"
          class="mar2t"
        >
          评选人{{ index2 + 1 }}：
          <Select
            transfer
            filterable
            filter-by-label
            class="w200"
            v-model="selectPeoples[item.name][index2]"
            @on-change="handleChangeSelectPeople(index, index2)"
          >
            <Option
              v-for="(people, pIndex) in checkOptionPeoples(item.name, index2)"
              :key="'num' + pIndex"
              :value="people.qq"
            >
              {{ people.nickname }}
            </Option>
          </Select>
          - [<a class="red" @click="doExport(index, index2)">下载</a>]
          {{ (answerObj = answerSelectedObj[index + "_" + index2]) && "" }}
          <template v-if="answerObj">
            - 数量：{{ answerObj.qqs.length }}
            <template v-if="answerObj.moveNum !== -1">
              - 作业挪到第 {{ answerObj.moveNum + 1 }} 位小伙伴
            </template>
          </template>
        </p>
      </div>
    </template>
  </div>
</template>

<script>
import { cloneObj, saveTXT } from "js/utils.js";
export default {
  props: ["questions", "answersObj", "analyResult"],
  data() {
    return {
      isInit: false,
      selectNums: 3,
      isExportAllQuestions: true,
      selectPeoples: {}, // 筛选人
      selectAllPeople: {}, // 所有题目的筛选人
      answerSelectedObj: {},
      questionNameObj: {},
      isAnonymousQuestions: true, // 匿名筛选
      isHaveNO: false, // 包含编号
    };
  },
  computed: {
    numbers() {
      return this.$store.getters.getMembers();
    },
    selectPeopleNums() {
      let num = this.selectNums;
      let list = [];
      for (let i = 0; i < num; i++) {
        list.push(i);
      }
      return list;
    },
    answerNum() {
      return Math.ceil(this.numbers.length / this.selectNums);
    },
  },
  watch: {
    selectNums() {
      this.initData();
    },
    analyResult: {
      handler() {
        this.initData();
      },
      deep: true,
    },
  },
  methods: {
    initData() {
      let normalObj = {};
      for (let i = 0; i < this.selectNums; i++) {
        normalObj[i] = "";
        this.$set(this.selectAllPeople, i, "");
      }
      for (let i = 0, n = this.questions.length; i < n; i++) {
        let question = this.questions[i];
        this.$set(this.selectPeoples, question.name, cloneObj(normalObj));
      }
      this.answerSelectedObj = {};
      this.isInit = true;
    },
    showQuestion(index) {
      this.$emit("showQuestion", index, this.isAnonymousQuestions);
    },
    handleChangeSelectPeople(index, index2) {
      let answerNum = this.answerNum;
      let name = "";
      if (index !== undefined) {
        name = this.questions[index].name;
      }
      let list = this.analyResult.filter((item) => item.count !== 0);
      list.sort((a, b) => (Math.random() > 0.5 ? 1 : -1));
      let list_len = list.length;
      let filterListObj = {};
      for (let numI = 0, n = this.selectPeopleNums.length; numI < n; numI++) {
        filterListObj[numI] = {
          moveNum: -1,
          qqs: [],
        };
      }
      let listI = 0;
      for (let numI = 0, n = this.selectPeopleNums.length; numI < n; numI++) {
        let selectQQ = name
          ? this.selectPeoples[name][numI]
          : this.selectAllPeople[numI];
        if (!selectQQ) {
          continue;
        }
        let filterList = [];
        for (let i = 0; i < answerNum && listI < list_len; i++) {
          let item = list[listI];
          listI += 1;
          if (item.qq === selectQQ) {
            let moveNum = numI + 1;
            if (moveNum === n) {
              moveNum = 0;
            }
            filterListObj[numI].moveNum = moveNum;
            filterListObj[moveNum].qqs.push(item.qq);
            continue;
          }
          filterList.push(item.qq);
        }
        filterListObj[numI].qqs.unshift(...filterList);
      }
      for (let numI = 0, n = this.selectPeopleNums.length; numI < n; numI++) {
        let name2 = `${index}_${numI}`;
        if (index === undefined) {
          name2 = numI + "";
        }
        this.$set(this.answerSelectedObj, name2, filterListObj[numI]);
      }
    },
    doExport(index, index2) {
      let name = "";
      let answersObj = this.answersObj;
      let selectPeopleObj = this.selectAllPeople;
      let answerSelectedObj = this.answerSelectedObj;
      let isAnonymousQuestions = this.isAnonymousQuestions;
      let isHaveNO = this.isHaveNO;
      let fileName = "";
      let fileNameExt = ".txt";
      let numbers = this.analyResult.filter((item) => item.count !== 0);
      let qqObj = {};
      for (let i = 0, n = this.numbers.length; i < n; i++) {
        let item = this.numbers[i];
        qqObj[item.qq] = item;
      }

      if (index !== null) {
        name = this.questions[index].name;
        selectPeopleObj = this.selectPeoples[name];
        fileName = this.questions[index].question + fileName;
      }
      if (index2 !== undefined) {
        let peopleQQ = selectPeopleObj[index2];
        if (!peopleQQ) {
          this.$Message.info("请先选择导出的人");
          return;
        }
        let qqItem = qqObj[peopleQQ];
        fileName = qqItem.nickname + "_" + fileName;
      }
      let str2 = "";
      if (index !== null) {
        str2 = getAnswers(name);
      } else {
        let questionNames = [];
        for (let i = 0, n = this.questions.length; i < n; i++) {
          let question = this.questions[i];
          questionNames.push(question.name);
          str2 += question.question + "\n";
          str2 += getAnswers(question.name);
          if (i !== n - 1) {
            str2 += "\n********************";
            str2 += "\n********************";
            str2 += "\n********************";
            str2 += "\n";
          }
        }
        fileName += questionNames.join("_");
      }
      function getAnswers(name) {
        let str = "";
        let list = answersObj[name];
        if (index2 !== undefined) {
          let name2 = "" + index2;
          if (index !== null) {
            name2 = index + "_" + name2;
          }
          let qqs = answerSelectedObj[name2].qqs;
          list = list.filter((item) => qqs.includes(item.qq));
        }
        for (let i = 0, n = list.length; i < n; i++) {
          let qq = list[i].qq;
          let item = qqObj[qq];
          let answer = list[i].answer;
          str += "\n********************";
          // str += "字数：" + answer.length;
          str += "\n";
          if (isAnonymousQuestions) {
            if (isHaveNO) {
              str += "编号:" + item.no;
            }
          } else {
            str += `${item.nickname}/${item.qq}/${item.time}`;
          }
          str += answer;
          if (answer.lastIndexOf("\n") === answer.length - 1) {
            str += "\n";
          }
        }
        return str;
      }
      saveTXT(str2, fileName + fileNameExt);
    },
    checkOptionPeoples(name, index2) {
      let extqqs = [];
      for (let i = 0; i < this.selectNums; i++) {
        if (i === index2) {
          continue;
        }
        let qq = name ? this.selectPeoples[name][i] : this.selectAllPeople[i];
        if (qq) {
          extqqs.push(qq);
        }
      }
      return this.numbers.filter((people) => !extqqs.includes(people.qq));
    },
  },
  mounted() {
    this.initData();
  },
};
</script>

<style></style>
